<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Admin\Mapper;

use App\Model\Order;
use App\Overwrites\AbstractMapper;
use Hyperf\Database\Model\Builder;
use Mine\MineModel;

/**
 * 订单列表Mapper类
 */
class OrderMapper extends AbstractMapper
{
    /**
     * @var Order
     */
    public $model;

    public function assignModel()
    {
        $this->model = Order::class;
    }

    public function read(mixed $id, array $column = ['*']): ?MineModel
    {
        $model = $this->model::with(['orderCar', 'orderLessee', 'orderInsurance'])->find($id, $column);
        if (is_null($model)) {
            return $model;
        }
        $model->orderInsurance->car_damage = sprintf('%s(%s)', $model->orderInsurance->car_damage, $model->orderInsurance->car_damage_sub);
        return $model;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        // 关联
        $query->with(['member:id,nickname', 'staff:id,name']);
        
        // 用户昵称
        if ( isset($params['member_nickname']) && filled($params['member_nickname']) ) {
            $query->whereHas('member', function (Builder $query) use ($params) {
                $query->where('nickname', 'like', '%'.$params['member_nickname'].'%');
            });
        }

        // 渠道ID
        if ( isset($params['channel_id']) && filled($params['channel_id']) ) {
            $query->where('channel_id', '=', $params['channel_id']);
        }

        // 员工姓名
        if ( isset($params['staff_name']) && filled($params['staff_name']) ) {
            $query->whereHas('staff', function (Builder $query) use ($params) {
                $query->where('name', 'like', '%'.$params['staff_name'].'%');
            });
        }

        // 订单号
        if ( isset($params['no']) && filled($params['no']) ) {
            $query->where('no', '=', $params['no']);
        }

        // 城市
        if ( isset($params['city']) && filled($params['city']) ) {
            $query->where('city', 'like', '%'.$params['city'].'%');
        }

        // 地址
        if ( isset($params['address']) && filled($params['address']) ) {
            $query->where('address', 'like', '%'.$params['address'].'%');
        }

        // 开始时间
        if ( isset($params['start_at']) && filled($params['start_at']) && count($params['start_at']) === 2 ) {
            $query->whereBetween(
                'start_at',
                [ $params['start_at'][0], $params['start_at'][1] ]
            );
        }

        // 结束时间
        if ( isset($params['end_at']) && filled($params['end_at']) && count($params['end_at']) === 2 ) {
            $query->whereBetween(
                'end_at',
                [ $params['end_at'][0], $params['end_at'][1] ]
            );
        }

        // 支付单号
        if ( isset($params['pay_no']) && filled($params['pay_no']) ) {
            $query->where('pay_no', '=', $params['pay_no']);
        }

        // 支付状态
        if ( isset($params['pay_status']) && filled($params['pay_status']) ) {
            $query->where('pay_status', '=', $params['pay_status']);
        }

        // 是否送达
        if ( isset($params['is_delivery']) && filled($params['is_delivery']) ) {
            $query->where('is_delivery', '=', $params['is_delivery']);
        }

        // 状态
        if ( isset($params['status']) && filled($params['status']) ) {
            $query->where('status', '=', $params['status']);
        }

        // 创建时间
        if ( isset($params['created_at']) && filled($params['created_at']) && count($params['created_at']) === 2 ) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        return $query;
    }

    public function handleExportItems($items)
    {
        /** @var Order $model */
        foreach ($items as $model) {
            $model->member_nickname = $model->member->nickname;
            $model->staff_name = $model->staff->name ?? '';
            $model->append(['pay_status_text', 'is_delivery_text', 'status_text']);
        }
    }
}